const app = getApp();

const _http =(method, url, data, response, error) =>{
    var reqData = {body:data}
    var loginToken = '';
    if(app.globalData.userInfo){
        loginToken = app.globalData.userInfo.loginToken
    }
    url = app.globalData.httpDomain + url;
    console.log("url = " +url+ "，loginToken = " + loginToken );
    console.log(reqData);
    my.request({
        method: method,
        url: url,
        data: reqData,
        headers: {
            'content-type': 'application/json',
            'authorization': loginToken,
        },
        success: res =>{
          if(res.data){
            if(res.data.code == 198){
              _login(method,url,reqData,response,error);
            }else{
                return response(res)
            }
          }else{
            if(error){
              return error()
            }
          }
        },
        fail:err =>{
            if(error){
                return error(err)
            }
        },
        complete:info =>{
        }
    });
}

const _login =(method, url, reqData, response, error) => {
  var loginToken = '';
  my.getAuthCode({
    scopes: ['auth_base'],
    success: res => {
      console.log(res)
      my.request({
          method: 'post',
          url: app.globalData.httpDomain + _url.login,
          data: {
            body:{
              code:res.authCode,
              appId:my.getAppIdSync().appId
            }
          },
          header: {
              'content-type': 'application/json',
              'authorization': loginToken
          },
          success:function(res){
            console.log(res.data);
            if(res.data.code == 200){
                app.globalData.userInfo = res.data.body;
                if(method && url){
                  loginToken = app.globalData.userInfo.loginToken;
                  my.request({
                    method: method,
                    url: url,
                    data: reqData,
                    headers: {
                        'content-type': 'application/json',
                        'loginToken': loginToken,
                    },
                    success: res =>{
                      return response(res)
                    },
                    fail:err =>{
                      if(error){
                        return error(err)
                      }
                    }
                  });
                }else{
                  return response(res)
                }
            }else{
              if(error){
                return error(res)
              }
            }
          }
      })
    }
    ,fail:res => {
      if(error){
        return error(err)
      }
    }
  })
}

function _isRepeat(lastTimes,num){
    if(lastTimes){
        let currentTime = new Date();
        if(currentTime - lastTimes < num) {
            return true;
        }
    }
    return false;
}

var _url = {
  login:'/xcx/v1/ali/login/check.do'
  ,userImgUp:'/xcx/v1/member/img/upload.do'  //上传用户图片
  ,userInfoUp:'/xcx/v1/member/info/update.do'//上传用户信息
  ,userPhoneRegister:'/xcx/v1/ali/phone/register.do'
  /**充电插座 */
  ,cdczScan:'/xcx/v1/cdcz/scan/get.do'               //获取设备信息
  ,payMeal:'/xcx/v1/cdcz/set_meal/pay.do'            //套餐支付
  ,cdczpay:'/xcx/v1/cdcz/charge_power/pay.do'        //充满即停支付
  ,cdczResult:'/xcx/v1/cdcz/pay/result.do'           //支付结果
  ,cdczClose:'/xcx/v1/cdcz/order/close.do'           //订单完成
  ,cdczWorking:'/xcx/v1/cdcz/order/working/get.do'  //工作中订单
  /**共享充电宝 */
  ,cdbScan:'/xcx/v1/cdb/scan/get.do'             //获取设备信息
  ,cdbPay:'/xcx/v1/cdb/lease/pay.do'             //充电宝支付
  ,cdbResult:'/xcx/v1/cdb/pay/result.do'         //支付结果
  /**便捷线充 */
  ,pclScan:'/xcx/v1/pcl/scan/get.do'             //获取设备信息
  ,pclPay:'/xcx/v1/pcl/lease/pay.do'             //支付
  ,pclResult:'/xcx/v1/pcl/pay/result.do'         //支付结果
  ,pclChangePwd:'/xcx/v1/pcl/pwd/change.do'         //切换密码
    /**便携式储能电池 */
  ,bxscnBatteryType:'/xcx/v1/bxscn/battery_type/find.data'      //储能电池类型列表
  ,bxscnBatteryInfo:'/xcx/v1/bxscn/battery_type/info.data'           //储能电池类型的租借信息
  ,bxscnBatteryPrice:'/xcx/v1/bxscn/battery_type/price.data'         //储能电池类型的租金
  ,bxscnBatteryShop:'/xcx/v1/bxscn/battery_type/shop/find.data'      //储能电池租借的网点列表
  ,bxscnBatteryPay:'/xcx/v1/bxscn/shop/lease/pay.do'                  //到店自提预约租借
  ,bxscnBatteryPayResult:'/xcx/v1/bxscn/shop/pay/result.do'

  ,bxscnBatteryOrderInfo:'/xcx/v1/member/bxscn_shop/order/info.data'       //电池门店订单详情
  ,bxscnBatteryOrderRevert:'/xcx/v1/member/bxscn_shop/order/revert.do'     //到店自提预约归还

  ,bxscnScan:'/xcx/v1/bxscn/scan/get.do'             //获取设备信息
  ,bxscnPay:'/xcx/v1/bxscn/lease/pay.do'             //便携式电池租借支付
  ,bxscnPayResult:'/xcx/v1/bxscn/pay/result.do'      //支付结果
  ,bxscnOpenSlot:'/xcx/v1/bxscn/slot/open.do'       //归还开仓
  ,bxscnOpenSlotResult:'/xcx/v1/bxscn/slot/open/result.do'   //归还开仓结果

  /**订单 */
  ,orderList:'/xcx/v1/member/order/list.data'        //订单列表
  ,orderInfo:'/xcx/v1/member/order/info.data'        //订单详情
  /**钱包 */
  ,walletGet:'/xcx/v1/member/wallet/get.do'       //钱包信息
  ,walletRecharge:'/xcx/v1/member/wallet/recharge.do'  //钱包充值
  ,walletRechargeResult:'/xcx/v1/member/wallet/recharge/result.do'  //钱包充值

  ,walletApply:'/xcx/v1/member/wallet/transfer_money/apply.do' //钱包提现
  ,walletGetList:'/xcx/v1/member/wallet/transfer_money/order/list.data' //钱包提现列表
  ,walletGetInfo:'/xcx/v1/member/wallet/transfer_money/order/info.data' //钱包提现详情

  // 优惠券
  ,couponList:'/xcx/v1/member/coupon/list.data'     //优惠券列表
  ,couponInfo:'/xcx/v1/coupon_activity/scan/get.do' //优惠券信息
  ,couponGet:'/xcx/v1/coupon_activity/receive.do'   //优惠券获取
  /*代理端*/
  ,agentBind:'/xcx/v1/member/agent/bind.do' //绑定账号

  /**常用设备 */
  ,useDeviceList:'/xcx/v1/member/common_device/list.data' //常用设备列表 0
  ,useDeviceAdd:'/xcx/v1/member/common_device/add.do'     //常用设备添加 0
  ,useDeviceDel:'/xcx/v1/member/common_device/del.do'     //常用设备删除 0

  /*通用*/
  ,commonSMS:'/xcx/v1/common/sendSMS.do' //发送短信
  ,commonNearPlace:'/xcx/v1/common/nearby_place/list.data' //附件网点
  ,commonPlaceRecommend:'/xcx/v1/common/place_recommendation/apply.do' //推荐场地
  ,commonAppConfig:'/xcx/v1/common/app_config/get.data' //获取应用配置
  ,agreementInfo:'/xcx/v1/common/agreement/get.data'
}

module.exports = {
  url:_url,
  isRepeat:_isRepeat,
  get:(url,data,response,error) => _http('GET',url,data,response,error),
  post:(url,data,response,error) => _http('POST',url,data,response,error),
  login:(response,error)=>_login(null,null,null,response,error)
}

